<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\DiscountOrder;
use Illuminate\Http\Request;
use App\Services\Admin\Binding;
use App\Services\Admin\Reservation;
use App\Services\Admin\User;
use App\Http\Requests\Admin\BindingController\doRule;

class BindingController extends Controller
{
    public function index()
    {

        $result = [
            'result'=>Binding::lists(),
            'sear_value'=>\request()->get('sear_value'),
            'is_enable'=>\request()->get('is_enable')
        ];

        return view('admin.binding.index', $result);

    }

    public function add()
    {
        $reservation_lists= Reservation::lists()->where('is_camp','2')->get()->toArray();

        array_multisort(array_column($reservation_lists,'class_quarterly_id'),SORT_DESC,$reservation_lists);

        foreach ($reservation_lists as $k=>$v) {
            $reservation_lists[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($v['class_base_info_teacher']);
        }

        $result = [
            'reservation_lists'=> $reservation_lists,
        ];

        return view('admin.binding.edit',$result);
    }

    public function edit(Request $request, $id)
    {
        $reservation_lists= Reservation::lists()->where('is_camp','2')->get()->toArray();

        array_multisort(array_column($reservation_lists,'class_quarterly_id'),SORT_DESC,$reservation_lists);

        foreach ($reservation_lists as $k=>$v) {
            $reservation_lists[$k]['teacher'] = Reservation::class_bass_info_teacher_arr($v['class_base_info_teacher']);
        }

        $result = [
            'id'=>$id,
            'result'=> Binding::info($id),
            'class_info_id'=>Binding::discount_info($id)->pluck('class_info_id')->toArray(),
            'reservation_lists'=> $reservation_lists,
        ];

        return view( 'admin.binding.edit', $result);
    }

    public function do(doRule $request)
    {
        if ($request->ajax()) {

            $input = $request->all();

            $cove = !empty($request->file('cove')) ? $request->file('cove') : [];

            if (!empty($cove)) {

                $path = $cove->store('images/'. date('Ymd'));

                $input['cove'] = "/upload/".$path;

            } else {

                unset($input['cove']);

            }

            $video = !empty($request->file('video')) ? $request->file('video') : [];

            if (!empty($video)) {

                $path = $video->store('/video/'.date('Ymd'));
                $input['video'] = "/upload/".$path;

            } else {

                unset($input['video']);

            }

            if (empty($input['id'])) {

                unset($input['id']);

                $group_result = Binding::add($input);

            } else {

                $group_result = Binding::edit($input['id'], $input);

            }

            if(!$group_result['bool'])return self::fail($group_result['message']);

            return self::success([]);
        }
    }

    public function del($id)
    {

        Binding::del($id);

        return self::success([]);

    }

    public function binding_exchange($id)
    {
        $user_ids = (new DiscountOrder)->where('discount_id',$id)->pluck('user_id')->toArray();

        $result = [
            'result'=>User::lists()->whereIn('id', $user_ids)->paginate(15),
            'sear_value'=>request()->sear_value??'',
        ];

        return view('admin.binding.user',$result);
    }
}
